package org.fullhappy.rest.entities;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.fullhappy.dao.CategoryDAO;
import org.fullhappy.dao.ContactInfoDAO;
import org.fullhappy.dao.GalleryDAO;
import org.fullhappy.dao.ProductDAO;
import org.fullhappy.entity.Category;
import org.fullhappy.entity.ContactInfo;
import org.fullhappy.entity.Gallery;
import org.fullhappy.entity.Product;
import org.fullhappy.rest.MediaType;
import org.fullhappy.rest.annotation.Consumes;
import org.fullhappy.rest.annotation.FormParam;
import org.fullhappy.rest.annotation.POST;
import org.fullhappy.rest.annotation.Path;
import org.fullhappy.rest.annotation.Produces;

import com.google.appengine.api.datastore.Text;

@Path("/process")
public class ActionProcess {

	// gallery{

	@POST
	@Path("/add_gallery")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public String addGallery(
			@FormParam("new_gallery_name") String newGalleryName) {
		GalleryDAO galleryDAO = new GalleryDAO();
		Gallery gallery = new Gallery();
		gallery.setDisable(false);
		gallery.setName(newGalleryName);
		try {
			galleryDAO.insert(gallery);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Gallery> list = galleryDAO.findAll();
		return "{'id':'" + gallery.getId() + "'}";
	}

	@POST
	@Path("/edit_gallery")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public String editGallery(@FormParam("gallery_id") long galleryId,
			@FormParam("new_gallery_name") String newGalleryName) {
		GalleryDAO galleryDAO = new GalleryDAO();
		Gallery gallery = galleryDAO.find(galleryId);
		gallery.setDisable(false);
		gallery.setName(newGalleryName);
		try {
			galleryDAO.update(gallery);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Gallery> list = galleryDAO.findAll();
		return "{'nn':'" + list.size() + "'}";
	}

	@POST
	@Path("/delete_gallery")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public String deleteGallery(@FormParam("gallery_id") long galleryId) {
		GalleryDAO galleryDAO = new GalleryDAO();
		Gallery gallery = galleryDAO.find(galleryId);
		try {
			galleryDAO.update(gallery);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Gallery> list = galleryDAO.findAll();
		return "{'nn':'" + list.size() + "'}";
	}

	// }gallery

	// category{
	@POST
	@Path("/add_category")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String addCategory(@FormParam("gallery_id") long galleryId,
			@FormParam("new_category_name") String newCategoryName) {
		CategoryDAO categoryDAO = new CategoryDAO();
		Category category = new Category();
		category.setGalleryId(galleryId);
		category.setName(newCategoryName);
		category.setDisable(false);
		try {
			categoryDAO.insert(category);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Category> list = categoryDAO.findAll();
		return "{'size':'" + list.size() + "'}";
	}

	@POST
	@Path("/edit_category")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String editCategory(@FormParam("category_id") long categoryId,
			@FormParam("new_category_name") String newCategoryName) {
		CategoryDAO categoryDAO = new CategoryDAO();
		Category category = categoryDAO.find(categoryId);
		category.setName(newCategoryName);
		category.setDisable(false);
		try {
			categoryDAO.update(category);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Category> list = categoryDAO.findAll();
		return "{'size':'" + list.size() + "'}";
	}

	@POST
	@Path("/delete_category")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String deleteCategory(@FormParam("category_id") long categoryId) {
		CategoryDAO categoryDAO = new CategoryDAO();
		Category category = categoryDAO.find(categoryId);
		try {
			categoryDAO.delete(category);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Category> list = categoryDAO.findAll();
		return "{'size':'" + list.size() + "'}";
	}

	// }category

	// product{
	@POST
	@Path("/add_product")
	// @Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	// 'category_id='+CategoryId+'&product_name='+productName+'&product_description="'+productDescription+'"&image_urls="'+imageURLs+'"'
	public String addProduct(@FormParam("category_id") Long categoryId,
			@FormParam("product_name") String productName,
			@FormParam("product_description") String productDescription,
			@FormParam("product_price") Long productPrice,
			@FormParam("image_urls") String imageURLs) {

		ProductDAO productDAO = new ProductDAO();
		Product product = new Product();
		product.setCategoryId(categoryId);
		product.setName(productName);
		product.setDisable(false);
		product.setCreatedTime(System.currentTimeMillis());
		product.setDescription(new Text(productDescription));
		product.setPrice(productPrice);
		product.setImageURLs(imageURLs);

		try {
			productDAO.insert(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "{'id':'" + product.getId() + "'}";
	}

	@POST
	@Path("/edit_product")
	// @Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	// 'category_id='+CategoryId+'&product_name='+productName+'&product_description="'+productDescription+'"&image_urls="'+imageURLs+'"'
	public String editProduct(@FormParam("product_id") Long productId,
			@FormParam("category_id") Long categoryId,
			@FormParam("product_name") String productName,
			@FormParam("product_description") String productDescription,
			@FormParam("product_price") Long productPrice,
			@FormParam("image_urls") String imageURLs) {

		ProductDAO productDAO = new ProductDAO();
		Product product = productDAO.find(productId);
		if (product != null) {
			if (categoryId != null) {
				product.setCategoryId(categoryId);
			}
			if (productName != null) {
				product.setName(productName);
			}
			if (productDescription != null) {
				product.setDescription(new Text(productDescription));
			}
			if (productPrice != null) {
				product.setPrice(productPrice);
			}
			if (imageURLs != null) {
				product.setImageURLs(imageURLs);
			}
		}

		try {
			productDAO.update(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "{'product_id':'" + product.getId() + "'}";
	}

	@POST
	@Path("/delete_product")
	// @Consumes(MediaType.MULTIPART_FORM_DATA)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("application/json")
	public String deleteProduct(@FormParam("category_id") Long categoryId) {

		ProductDAO productDAO = new ProductDAO();
		Product product = new Product();
		product.setCategoryId(categoryId);

		try {
			productDAO.delete(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "{'nn':'" + 11 + "'}";
	}

	// }product

	@POST
	@Path("/test1")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public void execute(@FormParam("someparameter") String param) {
		System.out.println(param);
	}

}
